import pymysql


class DBTools(object):
    @staticmethod
    def __db_connect():
        conn = pymysql.connect(host="211.103.136.244", user="student",
                               password="iHRM_student_2022", port=7061, database="ihrm", charset="utf8")
        return conn

    @staticmethod
    def db_select_one(sql):
        conn = None
        cursor = None
        try:
            conn = DBTools.__db_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            return cursor.fetchone()
        except Exception as err:
            print("出错了，错误类型为：", err)
        finally:
            cursor.close()
            conn.close()

    @staticmethod
    def db_select_many(number1, sql, number2):
        conn = None
        cursor = None
        try:
            conn = DBTools.__db_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            cursor.rownumber = number1
            return cursor.fetchmany(number2)
        except Exception as err:
            print("出错了，错误类型为：", err)
        finally:
            cursor.close()
            conn.close()

    @staticmethod
    def db_select_all(sql):
        conn = None
        cursor = None
        try:
            conn = DBTools.__db_connect()
            cursor = conn.cursor()
            cursor.rownumber = 0
            cursor.execute(sql)
            return cursor.fetchall()
        except Exception as err:
            print("出错了，错误类型为：", err)
        finally:
            cursor.close()
            conn.close()

    @staticmethod
    def db_iud(sql):
        conn = None
        cursor = None
        try:
            conn = DBTools.__db_connect()
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
            print("操作成功~")
        except Exception as err:
            print("出错了，错误类型为：", err)
            conn.rollback()
        finally:
            cursor.close()
            conn.close()


if __name__ == '__main__':
    DBTools.db_iud("delete from bs_user where mobile = 13800000012;")
    # print(DBTools.db_select_all("select * from t_book"))
    # print(DBTools.db_select_many(5, "select * from t_book", 5))